package com.xm9m.ibczy.dsp.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.xm9m.ibczy.dsp.https.HttpFactory;
import com.xm9m.ibczy.dsp.https.http.BCGetRequest;
import com.xm9m.ibczy.dsp.https.http.BCHttpListener;
import com.xm9m.ibczy.dsp.utils.AntLog;

import java.net.HttpURLConnection;

/**
 * Created by baichuan on 2016/11/29.
 */

public class BCImageView extends ImageView {
    private Bitmap bitmap;

    public BCImageView(Context context) {
        super(context);
    }

    public BCImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BCImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setUrl(final String url) {
        AntLog.i("three image url="+url);
        BCGetRequest get = new BCGetRequest(url);
        get.withListener(new BCHttpListener() {
            @Override
            public void onSuccess(int statueCode, byte[] data) {
                if (statueCode == HttpURLConnection.HTTP_OK) {
                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    AntLog.i("three image ok=="+url);
                    post(new Runnable() {
                        @Override
                        public void run() {
                            setImageBitmap(bitmap);
                        }
                    });
                }
            }

            @Override
            public void onError(int statueCode, String message) {

            }
        });
        HttpFactory.addRequest(get);
    }

}
